package com.hdss996.dictationread.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.hdss996.dictationread.R;
import com.hdss996.dictationread.util.StatusBarUtil;
import com.hdss996.dictationread.util.SysToolUtil;

public class DwnttsActivity extends AppCompatActivity {
    private SysToolUtil sysToolUtil = new SysToolUtil();
    private Context dc = DwnttsActivity.this;
    private ImageView backBtn;
    private Button dwn_xa_tts_btn;
    private Button dwn_xf_tts_btn;
    private Button dwn_gg_tts_btn;
    private Button dwn_dm_tts_btn;
    private Button dwn_tts_tts_btn;

    private static final String PACKAGE_TTS_SERVER_ANDROID = "com.github.jing332.tts_server_android";
    private static final String PACKAGE_XIAOMI_SPEECH = "com.xiaomi.mibrain.speech";
    private static final String PACKAGE_IFLYTEK_SPEECH = "com.iflytek.speechcloud";
    private static final String PACKAGE_GOOGLE_TTS = "com.google.android.tts";
    private static final String PACKAGE_BAIDU_OPENSDK = "com.baidu.duersdk.opensdk";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dwntts);
        StatusBarUtil.transparencyBar(this); //设置状态栏全透明
        StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
        init();

    }

    @Override
    protected void onStart() {
        super.onStart();
        checkAPPs();
    }

    private void checkAPPs() {
        // 检查特定应用是否存在，并根据存在与否设置对应按钮的文本
        checkApp(PACKAGE_TTS_SERVER_ANDROID, dwn_tts_tts_btn);
        checkApp(PACKAGE_XIAOMI_SPEECH, dwn_xa_tts_btn);
        checkApp(PACKAGE_IFLYTEK_SPEECH, dwn_xf_tts_btn);
        checkApp(PACKAGE_GOOGLE_TTS, dwn_gg_tts_btn);
        checkApp(PACKAGE_BAIDU_OPENSDK, dwn_dm_tts_btn);
    }

    private void init() {
        backBtn = findViewById(R.id.backBtn);
        dwn_xa_tts_btn = findViewById(R.id.dwn_xa_tts_btn);
        dwn_xf_tts_btn = findViewById(R.id.dwn_xf_tts_btn);
        dwn_gg_tts_btn = findViewById(R.id.dwn_gg_tts_btn);
        dwn_dm_tts_btn = findViewById(R.id.dwn_dm_tts_btn);
        dwn_tts_tts_btn = findViewById(R.id.dwn_tts_tts_btn);
        //返回主界面
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        dwn_tts_tts_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appControl(dwn_tts_tts_btn,"TTS Server语音引擎软件","com.github.jing332.tts_server_android");
            }
        });
        dwn_xa_tts_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                appControl(dwn_xa_tts_btn,"小爱语音引擎","com.xiaomi.mibrain.speech");
            }
        });
        dwn_xf_tts_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                appControl(dwn_xf_tts_btn,"讯飞语音引擎","com.iflytek.speechcloud");
            }
        });
        dwn_gg_tts_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                appControl(dwn_gg_tts_btn,"谷歌文字转语音引擎","com.google.android.tts");
            }
        });
        dwn_dm_tts_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                appControl(dwn_dm_tts_btn,"度秘语音引擎","com.baidu.duersdk.opensdk");
            }
        });

    }

    // 根据应用是否为系统应用设置按钮文本
    private void checkApp(String packageName, Button button) {
        if (sysToolUtil.isAppExist(packageName,dc)){
            setButtonText(button, packageName);
        }
    }

    private void setButtonText(Button button, String packageName) {
        if (sysToolUtil.isSystemApp(packageName, dc)) {
            button.setText("系统自带");
        } else {
            button.setText("卸载");
        }
    }

    public void appControl(Button button,String title,String pkgName) {
        if(button.getText().equals("下载")){
            SysToolUtil.dwnAlert("引擎下载","暂请自行百度下载"+title,"下载","https://www.baidu.com/",dc);
        }else if (button.getText().equals("系统自带")){
            SysToolUtil.msgAlert("提示", "这是手机系统自带的语音引擎！", "好的", dc);
        }else {
            SysToolUtil.msgAlert("提示", "请前往应用商店卸载！", "好的", dc);

            //DwnttsActivity.this.finish();
        }
    }
}

